layui.use(['form', 'admin', 'table', 'jquery', 'xmSelect'], function () {
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;
    var $ = layui.jquery;
    var xmSelect = layui.xmSelect;

    //渲染菜单树
    var leaderMenuXmSel = xmSelect.render({
        el: '#gen-menu-tree',
        name: 'searchParams[parentMenuId]',
        radio: true,
        clickClose: true,
        layVerify: 'required',
        layVerType: 'tips',
        filterable: true,
        tree: {
            show: true,
            strict: false,
            expandedKeys: [-1]
        },
        height: 350,
        data: allMenuTree
    });
    var parentMenuId = colTableData.parentMenuId;
    if (parentMenuId) {
        leaderMenuXmSel.setValue([parentMenuId]);
    }

    //渲染表格
    table.render({
        elem: '#colInfoTable',
        data: colTableData.columns,
        autoSort: true,
        page: true,
        toolbar: false,
        height: 'full-180',
        cols: [
            [
                {type: 'numbers', title: "序号", width: 50, fixed: 'left'},
                {field: 'columnName', title: '字段列名', width: '12%', fixed: 'left'},
                {field: 'columnType', title: '数据库类型', width: "10%"},
                {
                    field: 'columnComment', title: '描述（点击编辑）', width: '10%', edit: 'text', templet: function (d) {
                        return '<span style="color: #01AAED;">' + d.columnComment + '</span>';
                    }
                },
                {field: 'javaType', title: 'Java类型', width: "10%", templet: '#javaTypeTpl'},
                {
                    field: 'javaField', title: 'Java属性（点击编辑）', width: '10%', edit: 'text', templet: function (d) {
                        return '<span style="color: #01AAED;">' + d.javaField + '</span>';
                    }
                },
                {field: 'isInsert', title: "插入", width: 90, templet: '#insertTpl'},
                {field: 'isEdit', title: "编辑", width: 90, templet: '#editTpl'},
                {field: 'isList', title: "列表", width: 90, templet: '#listTpl'},
                {field: 'isQuery', title: "查询", width: 90, templet: '#queryTpl'},
                {field: 'isRequired', title: "必填", width: 90, templet: '#requiredTpl'},
                {field: 'queryType', title: '查询方式', width: "10%", templet: '#queryTypeTpl'},
                {field: 'htmlType', title: '显示类型', width: "12%", templet: '#htmlTypeTpl'},
                {
                    field: 'dictType', title: '字典类型', width: "16%", templet: function (d) {
                        var tplHtml = '<div class="ew-select-fixed">';
                        tplHtml += '<select lay-filter="typeTpl" column-id="' + d.columnId + '" field-name="dictType">';
                        tplHtml += '<option value="">请选择</option>';
                        dictTypes.forEach(function (dt) {
                            var selected = d.dictType === dt.dictType ? 'selected' : '';
                            var label = dt.dictType + '（' + dt.dictName + '）';
                            tplHtml += '<option value="' + dt.dictType + '"' + selected + '>' + label + '</option>';
                        });
                        tplHtml += '</select>';
                        return tplHtml;
                    }
                }
            ]
        ]
    });

    //监听单元格编辑
    table.on('edit(colInfoTable)', function (obj) {
        var value = obj.value;
        var data = obj.data
        var field = obj.field;
        var paramObj = {
            columnId: data.columnId
        };
        paramObj[field] = value;
        editColumn(paramObj);
    });

    //监听单元格下拉
    form.on('select(typeTpl)', function (data) {
        var $elem = data.elem;
        var selectValue = data.value;
        var columnId = $($elem).attr("column-id");
        var field = $($elem).attr("field-name");
        var paramObj = {
            columnId: columnId
        };
        paramObj[field] = selectValue;
        editColumn(paramObj);
    });

    //监听单元格单选
    form.on('switch(isTpl)', function (data) {
        var $elem = data.elem;
        var checkValue = data.elem.checked ? 1 : 0;
        var columnId = data.value;
        var field = $($elem).attr("name");
        var paramObj = {
            columnId: columnId
        };
        paramObj[field] = checkValue;
        editColumn(paramObj);
    });

    //其他配置保存
    form.on('submit(saveGenConfig)', function (data) {
        admin.req(prefix + "/edit",
            data.field,
            function (resp) {
                himio.notice.resp(resp, function () {
                    admin.closeThisTabs();
                });
            }, "post");
        return false;
    });

    /**
     * 编辑单元格（文本、下拉）
     * @param paramObj
     */
    function editColumn(paramObj) {
        admin.req(prefix + "/editColumn", paramObj, function (resp) {
            layer.msg(resp.msg);
        }, "post");
    }
});